package com.jiangli.controller.admin;

import com.github.pagehelper.Page;
import com.jiangli.annotation.Log;
import com.jiangli.dto.MerchantDTO;
import com.jiangli.dto.MerchantPageQueryDTO;

import com.jiangli.entity.Merchant;
import com.jiangli.exception.BaseException;
import com.jiangli.result.PageResult;
import com.jiangli.result.Result;
import com.jiangli.service.AdminMerchantService;
import com.jiangli.service.MerchantService;
import com.jiangli.vo.MerchantDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminMerchantController")
@RequestMapping("/admin/merchant")
@Slf4j
@Api(tags = "管理端商家接口")
public class MerchantController {
    @Autowired
    private AdminMerchantService adminMerchantService;

    @Autowired
    private MerchantService merchantService;
    @PostMapping()
    @ApiOperation("添加商家")
    @Log("添加商家")
    public Result addMerchant(MerchantDTO merchant) {
        try {
            adminMerchantService.addMerchant(merchant);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("加入失败");
        }
        return Result.success();
    }



    @DeleteMapping
    @ApiOperation("删除商家")
    @Log("删除商家")
    public Result deleteMerchant(Long merchantId) {
        try {
            adminMerchantService.delete(merchantId);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("删除失败");
        }
        return Result.success();
    }

    @PutMapping
    @ApiOperation("更新商家信息")
    @Log("更新商家信息")
    public Result updateMerchant(MerchantDTO merchant) {
        adminMerchantService.update(merchant);
        return Result.success();
    }


    @GetMapping("/detail")
    @ApiOperation("展示商家详细")
    public Result<MerchantDetailVO> getMerchantDetail(Long merchantId) {
        MerchantDetailVO merchantDetailVO = merchantService.showDetail(merchantId);
        return Result.success(merchantDetailVO);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询商家")
    public Result<PageResult> pageQuery(MerchantPageQueryDTO merchantPageQueryDTo) {
      PageResult pageResult=  adminMerchantService.pageQuery(merchantPageQueryDTo);
      return Result.success(pageResult);
    }

    @GetMapping("/query")
    @ApiOperation("根据商家名字查询")
    public Result<PageResult> show(MerchantDTO merchantDTO){
PageResult merchantList= adminMerchantService.query(merchantDTO);
       return Result.success(merchantList);
    }
}
